var pageNum;
$(function () {

    getQaTagList(1);

    $("body").on('click', '#btn1,#btn2', function () {
        //点击搜索查询从第一页开始查询
        getQaTagList(1);
    }).on('click', '#new-tag', function () {
        saveQaTag();
    }).on('click', '#delete-qaTag', function () {
        var obj = $("input.i-checks:checked");
        var tagIds = obtainIds(obj);
        if($.isEmptyObject(tagIds) || tagIds == ""){
            icModale(0,"请选择您要批量删除的标签");
        }else {
            icModale(2,"确定要删除所选标签吗？",3,function () {
                deleteQaTag(tagIds, obj);
            });
        }
    }).on('click', '.fa-trash-o', function () {
        var obj = $(this).parents("a");
        var tagIds = obj.data("id");
        icModale(2,"确定要删除该标签吗？",3,function () {
            deleteQaTag(tagIds, obj);
        });
    });

    function saveQaTag() {
        parent.layer.open({
            type: 1,
            area: ['380px'],
            skin: 'layui-layer-demo',
            title: '新建标签',
            content: '<div class="m-t m-r m-l m-b"><input class="form-control" id="qaTag" type="text"></div>',
            btn: '确定',
            yes: function(index, layero) {
                var tagName = layero.find('#qaTag').val();
                $.ajax({
                    url:baseloaction+"/admin/qa/qatag/saveQaTag",
                    type:"post",
                    data:{"tagName":tagName},
                    dataType:"json",
                    success:function (result) {
                        if (result.success == true) {
                            icModale(0,result.message)
                            parent.layer.close(index);
                            getQaTagList(1);
                            $("#qatag-count").html(parseInt($("#qatag-count").html()) + 1);
                        } else {
                            icModale(0,result.message);
                        }
                    },
                    error:function (error) {
                        icModale(0,"系统繁忙，请稍后重试");
                    }
                });
            }
        });
    }

    /**
     * 删除标签
     * @param tagIds 标签ID串
     * @param obj
     */
    function deleteQaTag(tagIds, obj) {
        var count = $("#qatag-count").html();
        $.ajax({
            url: baseloaction + "/admin/qa/qatag/deleteQaTag",
            data:{"tagIds": tagIds},
            type:"post",
            dataType:"json",
            success:function (result) {
                if(result.success){
                    icModale(0,result.message);
                    $("#qatag-count").html(count - obj.size());
                    getQaTagList(1);
                }else {
                    icModale(0,result.message);
                }
            },
            error:function () {
                icModale(0,"系统繁忙，请稍后重试");
            }
        });
    }

    /**
     * 获取问答标签列表
     * @param pageNo 当前页码
     */
    function getQaTagList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/qa/qatag/ajaxQueryQaTagList',//请求URL
            contextId: 'oc-ibox-content',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getQaTagList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-qaTag', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                pageNum = pageNo;
                $("#qatag-count").html(total);
            }
        };
        ajaxGetListData(obj);
    }
})


//拼接id串
function obtainIds(obj) {
    if(null == obj || obj.size() == 0){
        icModale(0,"请选择要操作的数据");
        return null;
    }
    var tagIds = "";
    $(obj).each(function (index,_input) {
        tagIds += _input.value + ",";
    })
    tagIds = tagIds.substring(0,tagIds.length-1);
    return tagIds;
}